package com.yun.controller;

import com.alibaba.excel.EasyExcel;
import com.aliyun.oss.OSSClient;
import com.yun.mapper.ArticleMapper;
import com.yun.pojo.Article;
import com.yun.service.ArticleService;
import com.yun.unit.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private Oss oss;
    @Autowired
    private UserListener userListener;
    @Autowired
    private ArticleMapper articleMapper;

    @PostMapping("/sel")
    @PreAuthorize("hasAnyAuthority('CHECKGROUP_QUERY')")//设置权限 只有有用权限才能调用这些方法
    public R selByid(@RequestBody PageUnit pageUnit){
        return articleService.selByUserId(pageUnit);
    }
    //假删文章
    @GetMapping("/del/{id}")
    public R del(@PathVariable Integer id){
        return articleService.updById(id);
    }

    //查看详情
    @GetMapping("/selXq/{id}")
    public R selXq(@PathVariable Integer id){
        return articleService.selById(id);
    }

    //根据文章名称进行查询
    @PostMapping("/selname")
    public R selByName(@RequestBody PageUnit pageUnit){
        return articleService.selByName(pageUnit);
    }

    //查询被假删掉的文章
    @PostMapping("/selj")
    public R selByDel(@RequestBody PageUnit pageUnit){
        return articleService.selByDel(pageUnit);
    }

    //还原或者彻底删除
    @GetMapping("/updj/{id}/{del}")
    public R updByDel(@PathVariable Integer id,@PathVariable Integer del){
        return articleService.updByDel(id,del);
    }

    //上传文章封面图片
    @PostMapping("/updImg")
    public R updateImg(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        System.out.println(originalFilename);
        String newname = UUID.randomUUID().toString().substring(24) + "-" + originalFilename;
        OSSClient xx = new OSSClient(oss.getUrl(), oss.getId(), oss.getSecret());
        xx.putObject(oss.getTong(), newname, file.getInputStream());
        String url1 = oss.getUrl();
        String[] split = url1.split("//");
        String url = split[0] + "//" + oss.getTong() + "." + split[1] + "/" + newname;
        return new R(Code.get_ok,"上传成功",url);
    }

    //新增文章
    @PostMapping("/inswen")
    public R insAll(@RequestBody Article article){
        return articleService.insAll(article);
    }

    //修改文章
    @PostMapping("/updwen")
    public R updAll(@RequestBody Article article){
        return articleService.updAll(article);
    }

    //上传文件
    @PostMapping("/updFile")
    public R updFile(MultipartFile file) throws IOException {
        InputStream inputStream = file.getInputStream();
        List<Article> objects = EasyExcel.read(inputStream)
                .registerReadListener(userListener).head(Article.class)
                .sheet(0).doReadSync();
        if (objects.size()>0){
            return new R(Code.save_ok,"上传成功");
        }
        return new R(Code.save_err,"上传失败");
    }
    //导出文章
    @RequestMapping("/expwen/{id}")
    public ResponseEntity<byte[]> expwen(@PathVariable Integer id){
        //查询登录这个人的所有文章
        List<Article> articles = articleMapper.selectAllById(id);
        //创建字节数据读流
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        //创建表格写入刚刚查回来的数据
        EasyExcel.write(baos,Article.class).sheet("文章管理").doWrite(articles);

        //给前端响应
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attachment;filename=文章管理.xlsx");
        return ResponseEntity.ok()
                .headers(headers)
                .body(baos.toByteArray());
    }
}
